package com.example.bookshop.service.user;

import com.example.bookshop.entity.Business;
import com.example.bookshop.entity.Customer;
import com.example.bookshop.entity.Order;
import com.example.bookshop.repository.BusinessRepository;
import com.example.bookshop.repository.CustomerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

//支付工具类
@Service
public class PayImpl implements Pay{
    private Order order;

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }

    @Autowired
    CustomerRepository customerRepository;
    @Autowired
    BusinessRepository businessRepository;

    public Integer payment(){
        Business business=businessRepository.findBusinessById(order.getBusinessId());
        Customer c1=customerRepository.findCustomerById(order.getCustomerId());
        Customer c2=customerRepository.findCustomerById(business.getCustomerId());
        if(c1.getId()!=c2.getId()){
            if(c1.getMoney()>=order.getPrice()){//判断用户A的余额是否大于购买订单金额
                float money= c1.getMoney()-order.getPrice();
                c1.setMoney(money);//将用户余额减少
                customerRepository.updateCustomer(c1);
                return 1;
            }
        }
        return 0;
    }
}
